import datetime
import json

import requests
import rsa
import base64

# import hashlib
""" 连接地址 """

# 发送post请求
remote = 'http://127.0.0.1:8090'

# r1 = requests.post("",data={key:value})
(pubk, prvk) = rsa.newkeys(1024);
pub = pubk.save_pkcs1()
print("pub:", pub)
# base64加密。成字符串
pub64 = str(base64.b64encode(pub), 'utf-8')
print("pub64:", pub64)
print('解密：')
prv = prvk.save_pkcs1()
d = datetime.datetime.now().strftime('%Y-%m-%d')
c = rsa.encrypt(d.encode('utf-8'), pubk)
print('登录与服务端建立连接')
print('发送公钥。')
headers = {
    "Content-Type": "application/json; charset=UTF-8",
}
data = json.dumps({"0": pub64})   #dumps：将python对象解码为json数据
'''发送公钥
   接收服务端的公钥
'''
r1 = requests.post(remote+'/auth/serverPub', data=data,headers=headers)
# print(r1)       # 状态
# print(r1.text)  # 返回的内容
# print(r1.content) # 整个流
respon = r1.json()
print('response:',respon)
"""保存此次服务器的密钥。"""
sPubKey = respon.get("data")
print('sPubkey',sPubKey)
print('接收密文')
print('解密密钥')
# m = hashlib.md5()
# b = a.enconde(encoding='UTF-8')
